//
//  ChangePicPassController.m
//  FinancialProduct
//
//  Created by Stella on 2017/9/1.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "ZSBntview.h"
#import "CKPuslichTool.h"
#import "ChangePicPassController.h"

@interface ChangePicPassController ()<ZSBntviewDelegate>{
    int count;
}
@property (weak, nonatomic) IBOutlet ZSBntview *BtnView;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel;
@property (weak, nonatomic) IBOutlet UIImageView *topImage;
@property (strong, nonatomic)NSString *passStr;
@end

@implementation ChangePicPassController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    count=0;
    _passStr=@"";
    self.reminderLabel.text=@"请绘制您的原手势密码";
    self.BtnView.delegate = self;
    
    self.topImage.image=[UIImage imageNamed:@"momo"];
    
}
- (IBAction)backTop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
//实现代理方法
-(void)zsbntview:(ZSBntview *)bntview moveResult:(NSString *)strM{
    
    
    
    //开启一个图形上下文
    UIGraphicsBeginImageContextWithOptions(bntview.frame.size, NO, 0.0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //截图
    [self.BtnView.layer renderInContext:ctx];
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图形上下文
    UIGraphicsEndImageContext();
    //把获取的图片保存到 imageview 中
    self.topImage.image = image;
    if (count==0) {
        NSString *oldPass=[[NSUserDefaults standardUserDefaults]objectForKey:@"PICPASSCODE"];
        
        //检查原密码是否正确
        if (![oldPass isEqualToString:strM]) {
            self.reminderLabel.text=@"原手势密码错误,请重新绘制";
        }else{
            count++;
            self.reminderLabel.text=@"请绘制您新的手势密码";
        }
    }else if (count==1){
        if (strM.length<4) {
            self.reminderLabel.text=@"手势密码请最少绘制4个点";
            return;
        }
        _passStr=strM;
        count++;
        self.reminderLabel.text=@"请再次绘制进行确认";
    }else{
        if (![_passStr isEqualToString:strM]) {
            self.reminderLabel.text=@"您两次绘制的不一致，请重新绘制";
            count=1;
            _passStr=@"";
        }else{
            [[NSUserDefaults standardUserDefaults]setObject:_passStr forKey:@"PICPASSCODE"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
   
        
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
